﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using AjaxControlToolkit;
using NET.Library.RegularExpressions;

namespace NET.Library.Web.Controls
{
    /// <summary>
    /// Custom Web Controls
    /// </summary>
    public class WebControls
    {
        /// <summary>
        /// LongDateRegularExpressionValidator Web Control
        /// </summary>
        [ToolboxData("<{0}:LongDateRegularExpressionValidator Text='LongDateRegularExpressionValidator' runat='server' display='None' setfocusonerror='true'></{0}:LongDateRegularExpressionValidator>")]
        public class LongDateRegularExpressionValidator : RegularExpressionValidator
        {
            /// <summary>
            /// Date validation control that checks for dates against the DateTime.ToLongDateString format.
            /// </summary>
            /// <remarks>The validation checks a value for the numeric day of the month, a space, the name of the month, a space, the numeric day of the month, and the year equivalent to the date value of this instance.</remarks>
            /// <example>
            /// 31 April 2006 will return an error.
            /// 29 February 2004 will be accepted.
            /// </example>
            public LongDateRegularExpressionValidator()
            {
                //string s_expression = "^((0[1-9])|(1[0-9])|(2[0-9])|(3[01]))\\s(January|March|May|July|August|October|December)\\s([0-9]{4})|((0[1-9])|(1[0-9])|(2[0-9])|(30))\\s(April|June|September|November)\\s([0-9]{4})|((0[1-9]|1[0-9]|2[0-8])\\s(February)\\s([0-9]{4}))|((29)\\s(February)\\s([02468][048]00))|((29)\\s(February)\\s([13579][26]00))|((29)\\s(February)\\s([0-9][0-9][0][48]))|((29)\\s(February)\\s([0-9][0-9][2468][048])|((29)\\s(February)\\s([0-9][0-9][13579][26])))$";
                this.Display = ValidatorDisplay.None;
                this.SetFocusOnError = true;
                //this.ValidationExpression = "^([0-9]{2}\\s)(January|February|March|April|May|June|July|August|September|October|November|December)\\s([0-9]{4})$";
                this.ValidationExpression = Regex.UKLongDate; //s_expression;
            }
        }

        /// <summary>
        /// DateRegularExpressionValidator Web Control
        /// </summary>
        [ToolboxData("<{0}:DateRegularExpressionValidator Text='DateRegularExpressionValidator' runat='server' display='None' setfocusonerror='true'></{0}:DateRegularExpressionValidator>")]
        public class DateRegularExpressionValidator : RegularExpressionValidator
        {
            /// <summary>
            /// Date validation control that checks for dates against the DateTime UK format.
            /// </summary>
            /// <remarks>The validation checks a value for the numeric day of the month, a slash /, the numeric day of the month, a slash / and the year equivalent to the date value of this instance.</remarks>
            /// <example>
            /// 31/04/2006 will return an error.
            /// 29/02/2004 will be accepted.
            /// </example>
            public DateRegularExpressionValidator()
            {
                this.Display = ValidatorDisplay.None;
                this.SetFocusOnError = true;
                this.ValidationExpression = Regex.UKDate; //dateExpression;
            }
        }

        /// <summary>
        /// DateMaskedEditValidator Web Control
        /// </summary>
        [ToolboxData("<{0}:DateMaskedEditValidator Text='DateMaskedEditValidator' runat='server' display='None' setfocusonerror='true'></{0}:DateRegularExpressionValidator>")]
        public class DateMaskedEditValidator : MaskedEditValidator
        {
            /// <summary>
            /// Date Masked Edit (AJAX Control Toolkit) validation control that checks for dates against the DateTime UK format.
            /// </summary>
            /// <remarks>The validation checks a value for the numeric day of the month, a slash /, the numeric day of the month, a slash / and the year equivalent to the date value of this instance.</remarks>
            /// <example>
            /// 31/04/2006 will return an error.
            /// 29/02/2004 will be accepted.
            /// </example>
            public DateMaskedEditValidator()
            {
                this.Display = ValidatorDisplay.None;
                this.SetFocusOnError = true;
                this.ValidationExpression = Regex.UKDate; //dateExpression;
                this.IsValidEmpty = false;
            }
        }

        /// <summary>
        /// CVRegularExpressionValidator Web Control
        /// </summary>
        [ToolboxData("<{0}:CVRegularExpressionValidator Text='CVRegularExpressionValidator' runat='server' display='None' setfocusonerror='true'></{0}:CVRegularExpressionValidator>")]
        public class CVRegularExpressionValidator : RegularExpressionValidator
        {
            /// <summary>
            /// CV File validation control that checks the extension of the file being uploaded.
            /// </summary>
            /// <remarks>The validation checks for file types of doc, txt, rtf, htm and pdf for CV File upload fields.</remarks>
            /// <example>
            /// A file with extension .jpg will return an error. 
            /// A file with extension .doc will be accepted.
            /// </example>
            public CVRegularExpressionValidator()
            {
                string s_expression = @".+\.(doc|txt|rtf|pdf)$";//@"^.+\.((doc)|(txt)|(rtf)|(htm)|(pdf))";
                this.Display = ValidatorDisplay.None;
                this.SetFocusOnError = true;
                this.ValidationExpression = s_expression;
                this.ErrorMessage = "CV files must be .doc, .txt, .rtf, .htm or .pdf";
            }
        }

        /// <summary>
        /// NINumberRegularExpressionValidator Web Control
        /// </summary>
        [ToolboxData("<{0}:NINumberRegularExpressionValidator Text='NINumberRegularExpressionValidator' runat='server' display='None' setfocusonerror='true'></{0}:NINumberRegularExpressionValidator>")]
        public class NINumberRegularExpressionValidator : RegularExpressionValidator
        {
            /// <summary>
            /// National Insurance Number validation control that checks for correct NI Number.
            /// </summary>
            /// <remarks>The first letter may not be D, F, I, Q, U or Z; the second letter may not be D, F, I, O, Q, U or Z; the final letter is optional.</remarks>
            /// <example>
            /// Matches JG103759A | AP019283D | ZX047829C
            /// Non-Matches DC135798A | FQ987654C | KL192845T 
            /// </example>
            public NINumberRegularExpressionValidator()
            {
                string s_expression = Regex.NationalInsuranceNumber;
                this.Display = ValidatorDisplay.None;
                this.SetFocusOnError = true;
                this.ValidationExpression = s_expression;
                this.ErrorMessage = "National Insurance Number is not valid";
            }
        }

        //public class EmailRegularExpressionValidator : RegularExpressionValidator
        //{

        //}

        /// <summary>
        /// Time DropDownList Web Control.
        /// </summary>
        [ToolboxData("<{0}:TimeDropDownList runat='server'></{0}:TimeDropDownList>")]
        public class TimeDropDownList : DropDownList
        {
            private string _SelectedTimeValue = "";

            /// <summary>
            /// Specify the Time value to show as selected in the DropDownList.
            /// </summary>
            public string SelectedTimeValue
            {
                get { return _SelectedTimeValue; }
                set { _SelectedTimeValue = value; }
            }
            /// <summary>
            /// A DropDownList Web Control that displays a list of times between 00:00 and 11:30
            /// in 30 minute intervals.
            /// </summary>
            public TimeDropDownList()
            {
                //ListItem liSelect = new ListItem("Select...", "");
                //this.Items.Add(liSelect);

                DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                for (int i = 0; i <= 47; i++)
                {
                    string timeToShow = time.ToShortTimeString();
                    ListItem li = new ListItem(timeToShow, timeToShow);
                    //Select the item?
                    if (li.Value == this.SelectedTimeValue)
                    {
                        li.Selected = true;
                    }

                    time = time.AddMinutes(30);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [ToolboxData("{0}:MonthsShortDropDownList runat=\"server\"></{0}:MonthsShortDropDownList>")]
        public class MonthsShortDropDownList : DropDownList
        {
            /// <summary>
            /// 
            /// </summary>
            public bool AddSelectText { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string SelectText { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string SelectValue { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public MonthsShortDropDownList()
            {

                if (this.AddSelectText)
                {
                    this.Items.Add(new ListItem(this.SelectText, this.SelectValue));
                }

                string[,] months = NET.Library.Types.DateTimes.MonthNamesShort();

                for (int i = 0; i < 12; i++)
                {
                    this.Items.Add(new ListItem(months[i, 2], months[i, 1]));
                }
            }
        }
    }
}

